#include "Client.h"
#include <iostream>
#include <fstream>
#include "account.h"
using namespace std;

Client::Client()
{

}

void Client::display_sp(int n, int m)
{
	system("cls");
	account ac;
	int k = 0;
	bool flag = false;
	ifstream inFile;
	inFile.open("account.dat", ios::binary);
	if (!inFile)
	{
		cout << "\n\n\t\t\tFile could not be open !! Press any Key...";
		cin.get();
		cin.get();
		return;
	}


	while (inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
	{
		if (ac.retacno() == n && ac.retpass() == m)
		{
			cout << "\n\t\t\tBALANCE DETAILS\n";
			ac.show_account();
			flag = true;
		}
		else if (ac.retacno() == n && ac.retpass() != m)
		{
			cout << "\n\n\t\t\tPassword is wrong.Please Try Again!!";
			k = 1;
			cin.get();
			cin.get();
		}
	}
	inFile.close();
	if (flag == false && k == 0)
		cout << "\n\n\t\t\tAccount number does not exist";
	cin.get();
	cin.get();
}

void Client::deposit_withdraw(int n, int option, int pass)
{
	system("cls");
	int amt;
	bool found = false;
	int k = 0;
	account ac;

	fstream File;
	File.open("account.dat", ios::binary | ios::in | ios::out);
	if (!File)
	{
		cout << "\n\n\t\t\tFile could not be open !! Press any Key...";
		cin.get();
		cin.get();
		return;
	}
	while (!File.eof() && found == false)
	{
		File.read(reinterpret_cast<char *> (&ac), sizeof(account));
		if (ac.retacno() == n && ac.retpass() == pass)
		{
			ac.show_account();
			if (option == 1)
			{
				system("cls");
				cout << "\n\n\t\t\t\tTO DEPOSITE AMOUNT: ";
				cout << "\n\n\t\t\tEnter The amount to be deposited: ";
				cin >> amt;
				ac.dep(amt);
			}
			if (option == 2)
			{
				system("cls");
				cout << "\n\n\t\t\t\tTO WITHDRAW AMOUNT: ";
				cout << "\n\n\t\t\tEnter The amount to be withdraw: ";
				cin >> amt;
				int bal = ac.retdeposit() - amt;
				if ((bal<500 && ac.rettype() == 'S') || (bal<1000 && ac.rettype() == 'C'))
					cout << "\n\n\t\t\tInsufficience balance";
				else
					ac.draw(amt);
			}
			int pos = (-1)*static_cast<int>(sizeof(ac));
			File.seekp(pos, ios::cur);
			File.write(reinterpret_cast<char *> (&ac), sizeof(account));
			cout << "\n\n\t\t\t\t Record Updated";
			found = true;
		}

		else if (ac.retacno() == n && ac.retpass() != pass)
		{
			cout << "\n\n\t\t\tPassword is wrong.Please Try Again!!";
			k = 1;
			cin.get();
			cin.get();
		}
	}
	File.close();
	if (found == false && k == 0)
		cout << "\n\n\t\t\t Record Not Found ";
	cin.get();
	cin.get();
}


Client::~Client()
{
}
